package com.rocketmq.producer.controller;

import com.cyn.aframework.constants.constant.RocketConstant;
import com.cyn.aframework.http.HmSyncHttpClientUtils;
import com.cyn.aframework.http.HttpClientUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/")
public class ProducerController {

    @Autowired
    private RocketMQTemplate mqTemplate;

    @GetMapping("/send1")
    public String send1(String msg) {
        setMessage(msg);
        return "";
    }

    @GetMapping("/send")
    public String send(String msg) {

//        setMessage(msg);

        String url = "http://192.168.109.132:18080/platform/successKilled/inserUser";
//        String url = "http://localhost:18080/platform/successKilled/inserUser";
//        String params = "index="+msg;
        Map<String,String> params = new HashMap<>();
        params.put("index",msg);
        String result = HmSyncHttpClientUtils.httpGet(url,params);
//        System.out.println(tmp);
        return "success";
    }

    @GetMapping("/send2")
    public String send2(String msg) {

//        setMessage(msg);

        String url = "http://192.168.109.132:18080/platform/successKilled/inserUser";
//        String url = "http://localhost:18080/platform/successKilled/inserUser";
        String params = "index="+msg;
        HttpClientUtils.httpConnManager(url,params);
//        System.out.println(tmp);
        return "success";
    }


    private void setMessage(String msg) {

        try {
            mqTemplate.syncSend(RocketConstant.Topic.SPRING_BOOT_TOPIC, MessageBuilder.withPayload(msg).build());

//            String tmp = HttpUtils.sendGet("",params);
//            mqTemplate.asyncSend(RocketConstant.Topic.SPRING_BOOT_TOPIC, msg, new SendCallback() {
//                @Override
//                public void onSuccess(SendResult sendResult) {
//
//                }
//
//                @Override
//                public void onException(Throwable throwable) {
//
//                }
//            });
//            mqTemplate.send(RocketConstant.Topic.SPRING_BOOT_TOPIC, MessageBuilder.withPayload(msg).build());
        }catch(Exception e){
            System.out.println("****************************");
            e.printStackTrace();
            System.out.println("****************************");
        }
    }

}
